package com.xh.bussiness.scheduletask;

import com.xh.bussiness.thirdapi.xiaoguanjia.bean.request.XgjBaseRequest;
import com.xh.bussiness.thirdapi.xiaoguanjia.service.IXiaoGuanJiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Component
public class XgjScheduleTask {

    @Value("${xgj.asyncTeachers}")
    private Boolean asyncTeachers;
    @Value("${xgj.asyncClasses}")
    private Boolean asyncClasses;
    @Value("${xgj.asyncStudents}")
    private Boolean asyncStudents;

    @Autowired
    private IXiaoGuanJiaService xiaoGuanJiaService;

    public static void main(String[] args) {
        LocalDate now = LocalDate.now().plusDays(-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        System.out.println(now.format(formatter));
    }

    // 每天凌晨一点半
    @Scheduled(cron = "0 30 1 * * ?")
    public void asyncTeachers(){
        if(!asyncTeachers){
            return;
        }
        // 获取前一天的日期
        LocalDate now = LocalDate.now().plusDays(-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        System.out.println(now.format(formatter));
        XgjBaseRequest bean = new XgjBaseRequest();
        bean.setUpdatetime(now.format(formatter));
        try {
            xiaoGuanJiaService.importTeacher(bean);
        } catch (Exception e) {
            e.printStackTrace();
            // 记录同步报错信息日志
        }
    }
    @Scheduled(cron = "0 0 2 * * ?")
    public void asyncClass(){
        if(!asyncClasses){
            return;
        }
        LocalDate now = LocalDate.now().plusDays(-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        XgjBaseRequest bean = new XgjBaseRequest();
        bean.setUpdatetime(now.format(formatter));
        try {
            xiaoGuanJiaService.importClass(bean, true, true);
        } catch (Exception e) {
            e.printStackTrace();
            // 记录同步报错信息日志
        }
    }
    @Scheduled(cron = "0 30 2 * * ?")
    public void asyncStudents(){
        if(!asyncStudents){
            return;
        }
        LocalDate now = LocalDate.now().plusDays(-1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        XgjBaseRequest bean = new XgjBaseRequest();
        bean.setUpdatetime(now.format(formatter));
        try {
            xiaoGuanJiaService.importStudent(bean);
        } catch (Exception e) {
            e.printStackTrace();
            // 记录同步报错信息日志
        }
    }

}
